package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.query.JsonResult;
import cn.wolfcode.query.EmployeeQueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.StringUtil;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
	@Autowired
	private IEmployeeService employeeService;
	@Autowired
	private IDepartmentService departmentService;
	@Autowired
	private IRoleService roleService;

	@RequestMapping("/importXls")
	@ResponseBody
	public JsonResult importXls(MultipartFile file) throws IOException {
		employeeService.importXls(file);
		return new JsonResult();
	}

	@RequestMapping("/exportXls")
	public void exportXls(HttpServletResponse response){
		// 响应文件头,设置文件名字
		response.setHeader("Content-Disposition","attachment;filename=employee.xls");
		// 创建excel 文件
		Workbook wb = employeeService.exportXls();
		try {
			// 把excel的数据输出给浏览器
			wb.write(response.getOutputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@RequestMapping("/checkName")
	@ResponseBody
	public Map checkName(Long id, String name){
		Map<String,Object> map = new HashMap<>();
		// 根据id 和名字查询员工对象
		Employee employee = employeeService.selectByUsernameAndId(name,id);
		// 如果查询出来的员工对象不为空并且名字和查出来的一样,允许修改
		if(id != null && employee != null){
			map.put("valid",true);
			return map;
		}
		// 没有id 或者名字没重复
		// 否则不根据id 查询
		map.put("valid",employeeService.selectByUsernameAndId(name,null)==null);
		return map;
	}

	@RequestMapping("/status")
	@ResponseBody
	public JsonResult status(Long id){
		try{
			employeeService.updateStatus(id);
			return new JsonResult();
		}catch(Exception e){
			e.printStackTrace();
			return new JsonResult(false,e.getMessage());
		}
	}

	@RequestMapping("/resetPwd")
	@ResponseBody
	public JsonResult resetPwd(Long id,String newPassword){
		if(!StringUtil.hasLength(newPassword)){
			return new JsonResult(false,"请确保输入完整");
		}
		try {
			employeeService.updatePassword(id,newPassword);
			return new JsonResult();
		}catch (Exception e){
			return new JsonResult(false,"重置失败");
		}
	}

	@RequestMapping("/inputResetPwd")
	public String inputResetPwd(Long id,Model model) {
		model.addAttribute("employee",employeeService.get(id));
		return "/password/resetPwd";
	}

	@RequestMapping("/updatePwd")
	@ResponseBody
	public JsonResult updatePwd(Long id,String oldPassword,String newPassword){
		Employee employee = UserContext.getCurrentUser();
		if(!StringUtil.hasLength(oldPassword)|| !StringUtil.hasLength(newPassword)){
			return new JsonResult(false,"请确保输入完整");
		}
		if(!employee.getPassword().equals(new Md5Hash(oldPassword, employee.getName()).toString())){
			return new JsonResult(false,"请确保原密码输入正确");
		}
		try {
			employeeService.updatePassword(id,newPassword);
			return new JsonResult();
		}catch (Exception e){
			return new JsonResult(false,"修改失败");
		}
	}

	@RequestMapping("/inputUpdatePwd")
	public String inputUpdatePwd() {
		return "/password/updatePwd";
	}

	@RequestMapping("/saveOrUpdate")
	@ResponseBody
	public JsonResult saveOrUpdate(Employee employee, Long[] ids, String repassword){
		if(employee.getId() != null){
			employeeService.update(employee,ids);
		}else {
			employeeService.insert(employee,ids);
		}
		return new JsonResult();
	}

	@RequestMapping("/input")
	public String input(Long id,Model model){
		if(id != null){
			model.addAttribute("employee",employeeService.get(id));
		}
		model.addAttribute("departments",departmentService.selectAll());
		model.addAttribute("roles",roleService.selectAll());
		return "/employee/input";
	}

	@RequestMapping("/batchDelete")
	@ResponseBody
	public JsonResult batchDelete(Long[] ids){
		if(ids != null){
			employeeService.batchDelete(ids);
		}
		return new JsonResult();
	}

	@RequestMapping("/delete")
	@ResponseBody
	public JsonResult delete(Long id){
		if(id != null){
			employeeService.delete(id);
		}
		return new JsonResult();
	}

	@RequestMapping("/list")
	public String list(@ModelAttribute("qo")EmployeeQueryObject qo, Model model){
		PageInfo<Employee> pageInfo = employeeService.query(qo);
		model.addAttribute("pageInfo",pageInfo);
		model.addAttribute("departments",departmentService.selectAll());
		return "/employee/list";
	}
}
